from typing import Generic, TypeVar, Optional
from pydantic import BaseModel

T = TypeVar('T')


class BaseResponse(BaseModel, Generic[T]):
    code: int = 200
    message: str = "success"
    data: Optional[T] = None

    @classmethod
    def success(cls, data: T = None, message: str = "success") -> 'BaseResponse[T]':
        return cls(
            code=200,
            message=message,
            data=data
        )

    @classmethod
    def error(cls, code: int = 400, message: str = "error", data: T = None) -> 'BaseResponse[T]':
        return cls(
            code=code,
            message=message,
            data=data
        )
